package com.example.svnkit_high_api.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNCommitClient;
import org.tmatesoft.svn.core.wc.SVNWCClient;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 此类是高层次的SVN api 但是底层还是调用的low-api，操作editor
 * @author Shenshuxin
 */
@Service
public class SVNHighService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * 此方法是把文件上传到svn
     * @param svnUrl svn的地址信息
     * @param text 待上传的文本内容
     * @return 返回map类型，给controller返回
     */
    public Map upstreamSVN(String svnUrl,String text){
        if (text==null || "".equals(text)){
            logger.debug("待上传的文本内容为空！");
            return null;
        }else if (svnUrl==null || "".equals(svnUrl)){
            logger.debug("svn路径为空！");
            return null;
        }
        logger.debug("开始上传到svn");
        try {

            SVNClientManager svnClientManager = SVNClientManager.newInstance(new DefaultSVNOptions(), "user", "pass");
            SVNWCClient wcClient = svnClientManager.getWCClient();
            wcClient.doAdd(new File("本地磁盘uri"), true, true, true, SVNDepth.INFINITY, true, true);
            SVNCommitClient commitClient = svnClientManager.getCommitClient();
            HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
            return objectObjectHashMap;
        }catch (Exception e){
            logger.error("上传失败！");
            return null;
        }
    }

}
